package edu.scau.mis.rest.service.impl;

import edu.scau.mis.rest.entity.RestUser;
import edu.scau.mis.rest.mapper.RestUserMapper;
import edu.scau.mis.rest.service.RestUserService;
import edu.scau.mis.sys.entity.SysRole;
import edu.scau.mis.sys.mapper.SysRoleMapper;
import edu.scau.mis.sys.mapper.SysUserRoleMapper;
import org.springframework.stereotype.Service;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.PageRequest;

import javax.annotation.Resource;

/**
 * (RestUser)表服务实现类
 *
 * @author makejava
 * @since 2022-03-11 15:20:12
 */
@Service("restUserService")
public class RestUserServiceImpl implements RestUserService {
    @Resource
    private RestUserMapper restUserMapper;

    @Resource
    private SysUserRoleMapper sysUserRoleMapper;

    @Resource
    private SysRoleMapper sysRoleMapper;

    /**
     * 通过ID查询单条数据
     *
     * @param id 主键
     * @return 实例对象
     */
    @Override
    public RestUser queryById(Long id) {
        return this.restUserMapper.queryById(id);
    }

    /**
     * 分页查询
     *
     * @param restUser 筛选条件
     * @param pageRequest      分页对象
     * @return 查询结果
     */
    @Override
    public Page<RestUser> queryByPage(RestUser restUser, PageRequest pageRequest) {
        long total = this.restUserMapper.count(restUser);
        return new PageImpl<>(this.restUserMapper.queryAllByLimit(restUser, pageRequest), pageRequest, total);
    }

    /**
     * 新增数据
     *
     * @param restUser 实例对象
     * @return 实例对象
     */
    @Override
    public RestUser insert(RestUser restUser) {
        this.restUserMapper.insert(restUser);
        return restUser;
    }

    /**
     * 修改数据
     *
     * @param restUser 实例对象
     * @return 实例对象
     */
    @Override
    public RestUser update(RestUser restUser) {
        this.restUserMapper.update(restUser);
        return this.queryById(restUser.getId());
    }

    /**
     * 通过主键删除数据
     *
     * @param id 主键
     * @return 是否成功
     */
    @Override
    public boolean deleteById(Long id) {
        return this.restUserMapper.deleteById(id) > 0;
    }

    /**
     * 通过openid查询用户
     *
     * @param openid
     * @return
     */
    @Override
    public RestUser queryRestUserByOpenid(String openid){return this.restUserMapper.queryRestUserByOpenid(openid);}

    /**
     * 新建用户
     *
     * @param openid
     * @return
     */
    @Override
    public int insertRestUser(String openid){
        //新增用户
        RestUser restUser = new RestUser();
        restUser.setOpenid(openid);

        return this.restUserMapper.insert(restUser);
    }
}
